package com.jason.bookworm.utils;

import com.jason.bookworm.dao.pojo.LoginUser;
import com.jason.bookworm.dao.pojo.User;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

//spring-security用户权限工具类
//只能在启用spring-security时调用
public class LoginUserUtil {
    //获取当前用户的用户名
    public static String getCurrentUsername() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        return loginUser.getUsername();
    }

    //获取当前登录用户
    public static LoginUser getLoginUser(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return (LoginUser) authentication.getPrincipal();
    }

    //获取当前登录用户的详细信息
    public static User getLoginUserInfo(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        return loginUser.getUser();
    }
}
